using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Resources;

namespace resx_utils
{

	public class Utils
    {

		public static SortedDictionary<String, Object> CreateSortedDictionary(String resxFile)
		{
			FileStream fs = new FileStream(resxFile, FileMode.Open, FileAccess.Read, FileShare.Read);
			// Create resource readers
			ResXResourceReader resxIn = new ResXResourceReader(fs);

			// Create an IDictionaryEnumerator to iterate through the resources.
			IDictionaryEnumerator nodes = resxIn.GetEnumerator();
			SortedDictionary<String, Object> dict = new SortedDictionary<string, object>();

			while (nodes.MoveNext())
			{
				dict.Add(nodes.Key.ToString(), nodes.Value);
			}

			resxIn.Close();
			fs.Close();

			return dict;
		}

		public static void Compare(SortedDictionary<String, Object> dict1, SortedDictionary<String, Object> dict2)
		{
			SortedDictionary<String, Object>.Enumerator enum1 = dict1.GetEnumerator();
			SortedDictionary<String, Object>.Enumerator enum2 = dict2.GetEnumerator();

			bool hasMore1, hasMore2;

			hasMore1 = enum1.MoveNext();
			hasMore2 = enum2.MoveNext();

			while(hasMore1 && hasMore2)
			{
				int cmpKey = enum1.Current.Key.CompareTo(enum2.Current.Key);
				if (cmpKey == 0)
				{
					int cmpValue = enum1.Current.Value.ToString().CompareTo(enum2.Current.Value.ToString());
					if(cmpValue != 0)
					{
						Console.WriteLine("{0}:\n\t1={1}\n\t2={2}", enum1.Current.Key, enum1.Current.Value, enum2.Current.Value);
					}
					hasMore1 = enum1.MoveNext();
					hasMore2 = enum2.MoveNext();
				}
				else if (cmpKey < 0)
				{
					Console.WriteLine("1 <<< {0}", enum1.Current.Key);
					hasMore1 = enum1.MoveNext();
				}
				else
				{
					Console.WriteLine("2 <<< {0}", enum2.Current.Key);
					hasMore2 = enum2.MoveNext();
				}
			}

			while (hasMore1)
			{
				Console.WriteLine("2 <<< {0}", enum1.Current.Key);
				hasMore1 = enum1.MoveNext();
			}

			while (hasMore2)
			{
				Console.WriteLine("1 <<< {0}", enum2.Current.Key);
				hasMore2 = enum2.MoveNext();
			}

		}

        public static bool VerifyExistance(String file)
        {
            if (!File.Exists(file))
            {
                Console.WriteLine("\nERROR: {0} does not exist.");
                return false;
            }
            return true;
        }

	}

}
